package com.tanhua.bg.controller;

import com.tanhua.bg.service.UserManagerService;
import com.tanhua.bg.vo.PageResult;
import com.tanhua.bg.vo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zongjie
 * @Description: 用户管理controller
 * @date 2021/10/13  9:41
 */
@RestController
@RequestMapping("manage")
public class UserManagerController {
    @Autowired
    private UserManagerService userManagerService;



    /**
     *   用户数据翻页接口(查询所有用户)
     * @param page      当前页
     * @param pageSize  每页显示条数
     * @param userId     根据用户id查询的条件
     * @param nickname   根据昵称查询的条件
     * @param city       根据城市查询的条件
     * @return
     */
    @GetMapping("users")
    public ResponseEntity<PageResult> queryUsers(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                 @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "id",required = false) Integer userId,
                                                 @RequestParam(value = "nickname",required = false) String nickname,
                                                 @RequestParam(value = "city",required = false) String city
    ) {
        try {
            // 调用service  返回分页结果对象
            PageResult pageresult = userManagerService.queryUsers(page,pageSize,userId,nickname,city);
            if (pageresult != null){
                // 结果不为null  直接返回
                return ResponseEntity.ok(pageresult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回500
        return ResponseEntity.status(500).build();
    }

    @GetMapping("users/{userID}")
    public ResponseEntity<UsersVo> queryUserInfoById(@PathVariable("userID")Long userId){
        try{
           UsersVo usersVo = userManagerService.queryUserInfoById(userId);
            if(usersVo != null){
                return ResponseEntity.ok(usersVo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }


    /**
     *  用户冻结操作
     * @param param
     * @return
     */
    @PostMapping("users/freeze")
    public ResponseEntity<String> freezingUser(@RequestBody Map<String,Object> param){
        //  调用service
        Boolean result = userManagerService.freezingUser(param);
        if (result){
            // 冻结成功
            return ResponseEntity.ok("冻结成功");
        }
        // 冻结失败
        return ResponseEntity.status(500).body("冻结失败");
    }


    /**
     *  用户解冻操作
     * @param param
     * @return
     */
    @PostMapping("users/unfreeze")
    public ResponseEntity<String> unfreezingUser(@RequestBody Map<String,Object> param) {
        //  调用service
        Boolean result = userManagerService.unfreezingUser(param);
        if (result){
            // 解冻成功
            return ResponseEntity.ok("解冻成功");
        }
        // 冻结失败
        return ResponseEntity.status(500).body("解冻失败");
    }
}
